<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="../../include/tag.jsp" %>
<style type="text/css">
    .info-div {
        font-size: 16px;
        color: #7f7f7f;
        flex: 1;
    }

    #settingsDialog .layui-form-item .layui-input-inline {
        width: 100px!important;
    }
</style>
<div class="f-search-bar" style="border: 1px solid #dbdee5; padding: 16px; border-radius: 5px; margin-top: 20px">
    <div class="search-container" style="display: flex !important;">
        <p class="mb5 info-div">余额：
            <span class="red" style="font-size: 24px; font-family: '微软雅黑';">
                <fmt:formatNumber pattern="#,###.##" value="${totalPayment}"/>
            </span>&nbsp;元
        </p>
        <p class="mb5 info-div">短信剩余数量：
            <span style="font-size: 24px; font-family: '微软雅黑';">${packageCount.totalNum < packageCount.useNum ? 0 : packageCount.totalNum - packageCount.useNum}</span>&nbsp;条
            <a href="javascript:recharge()" target="_self" class="layui-btn layui-btn-danger layui-btn-sm ml10">
                充值</a>
            <a href="javascript:countSettings()" target="_self" class="layui-btn layui-btn-danger layui-btn-sm ml10">
                配置</a>
        </p>
        <c:if test="${packageCount.useNum - packageCount.totalNum > 0}">
            <p class="mb5 info-div">短信超出数量：
                <span style="font-size: 24px; font-family: '微软雅黑';">${packageCount.useNum - packageCount.totalNum}</span>&nbsp;条
            </p>
        </c:if>
        <p class="mb5 info-div">
            <span style="vertical-align: sub;">
                短信收费状态：${packageCount.isOpen == 1 ? '已开启' : ''}
                ${empty packageCount.id || packageCount.isOpen == 2 ? '已关闭' : ''}
            </span>
        </p>
        <c:if test="${packageCount.isOpen == 1}">
            <p class="mb5 info-div">
                <span style="vertical-align: sub;">
                    短信允许超出状态：${packageCount.isAllowExcess == 1 ? '已开启' : ''}
                        ${packageCount.isAllowExcess == 2 ? '已关闭' : ''}
                </span>
            </p>
        </c:if>
    </div>
</div>
<script type="text/javascript">
    txz.initHeader({
        nav:[{
            name: '合作商管理',
            href: '${ctx}/system/member/partnerList.do'
        },{
            name: '${member.realname}'
        },{
            name: '查看合作商信息',
            curr: true
        }],
        btns:[{
            name: '返回',
            href: 'javascript:history.back();',
            icon: 'back'
        }]
    });
    var form = null, element = null;
    $(function () {
        layui.use(['element', 'form'], function () {
            element = layui.element, form = layui.form;

            element.on('tab', function (data) {
                if (data.index == 0) {
                    location.href = "${ctx}/order/order/memberOrderList.do?mmId=${member.id}";
                } else if (data.index == 1) {
                    location.href = "${ctx}/wallet/withdrawals/memberWithdrawList.do?mmId=${member.id}";
                }
            });

            form.render('radio');
        });
    });

    /**
     * 短信充值
     */
    function recharge() {
        var index = layer.open({
            type: 1,
            area: '350px',
            title: '短信充值',
            content: $("#rechargeDialog"),
            btn: ['确定', '取消'],
            success: function () {
                $("#rechargeDialog [name=quantity]").val("");
            },
            end: function () {
                $("#rechargeDialog [name=quantity]").val("");
            },
            yes: function () {
                var msg = "";
                var value = $("#rechargeDialog [name=quantity]").val();
                if (value == "") {
                    msg = "短信充值数量不能为空";
                } else if (!util.checkNumber(value)) {
                    msg = "请输入正确的数字";
                } else if (parseInt(value) == 0) {
                    msg = "请输入大于0的数字";
                }
                if (msg != "") {
                    util.layerMsgError(msg);
                    return null;
                }

                var index = layer.load(1);
                $.post("${ctx}/member/packageCount/recharge.do", $("#rechargeDialog form").serializeArray(), function (ret) {
                    layer.close(index);
                    if (ret.success == true) {
                        util.layerMsgSuccess('充值成功', function () {
                            window.location.reload();
                        });
                    } else {
                        util.layerMsgError('充值失败');
                    }
                });
            }
        });
    }

    /**
     * 短信配置
     */
    function countSettings() {
        var index = layer.open({
            type: 1,
            area: '370px',
            title: '短信配置',
            content: $("#settingsDialog"),
            btn: ['提交', '取消'],
            success: function () {
                $("#settingsDialog").find("[name=isOpen][value='${packageCount.isOpen}']").prop('checked', true);
                $("#settingsDialog").find("[name=isAllowExcess][value='${packageCount.isAllowExcess}']").prop('checked', true);
                $("#settingsDialog").find("[name=warnNum]").val('${packageCount.warnNum}');
                $("#settingsDialog").find("[name=excessNum]").val('${packageCount.excessNum}');
                form.render('radio');
            },
            end: function () {
                $("#settingsDialog").find("[name=isOpen]").prop('checked', false);
                $("#settingsDialog").find("[name=isAllowExcess]").prop('checked', false);
                $("#settingsDialog").find("[name=warnNum]").val('');
                $("#settingsDialog").find("[name=excessNum]").val('');
            },
            yes: function () {
                var isOpen = $("#settingsDialog").find("[name=isOpen]:checked").val();
                var isAllowExcess = $("#settingsDialog").find("[name=isAllowExcess]").val();
                var warnNum = $("#settingsDialog").find("[name=warnNum]").val();
                var excessNum = $("#settingsDialog").find("[name=excessNum]").val();
                if (isOpen == undefined || isOpen == "") {
                    util.layerMsgError('请选择开关控制');
                    return null;
                }

                if (isAllowExcess == undefined || isAllowExcess == "") {
                    util.layerMsgError('请选择允许超出开关控制');
                    return null;
                }

                if (warnNum == "") {
                    util.layerMsgError("请输入预警数量");
                    return null;
                } else if (!util.checkNumber(warnNum)) {
                    util.layerMsgError("请输入正确的数字");
                    return null;
                }

                /*if (excessNum == "") {
                    layer.msg("请输入允许超出数量");
                    return null;
                } else if (!util.checkNumber(excessNum)) {
                    layer.msg("请输入正确的数字");
                    return null;
                }*/

                var index = layer.load(1);
                $.post("${ctx}/member/packageCount/saveSettings.do", $("#settingsDialog form").serializeArray(), function (ret) {
                    layer.close(index);
                    if (ret.success == true) {
                        util.layerMsgSuccess("提交成功", function () {
                            window.location.reload();
                        });
                    } else {
                        util.layerMsgError("提交失败")
                    }
                });
            }
        });
    }
</script>
